package com.xuequ.answerme.question;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.QuestionBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.manager.QuestionManager;

import cn.bmob.v3.BmobUser;

/**
 * Created by Shijie on 2015/10/1.
 */
public class AskQuestionActivity extends AppCompatActivity {

    private Toolbar toolbar;

    private CheckBox mAnonymousCheckBox;
    private ImageView mAskImg;
    private EditText mQuestionTitleEditText;
    private EditText mQuestionDescEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        initViews();
    }

    private void initViews() {

        toolbar = (Toolbar) findViewById(R.id.ask_question_tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mAnonymousCheckBox = (CheckBox) findViewById(R.id.ask_question_anonymousAsk_checkBox);
        mAskImg = (ImageView) findViewById(R.id.ask_question_ask_imageView);
        mQuestionTitleEditText = (EditText) findViewById(R.id.ask_question_title_editText);
        mQuestionDescEditText = (EditText) findViewById(R.id.ask_question_description_editText);

        mAskImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(mQuestionTitleEditText.getText())){
                    mQuestionTitleEditText.setHint("请填写你的问题并用问号结尾");
                } else {
                    QuestionBean questionBean = new QuestionBean();

                    questionBean.setQuestion_title(mQuestionTitleEditText.getText().toString());
                    questionBean.setQuestion_description(mQuestionDescEditText.getText().toString());
                    if(mAnonymousCheckBox.isChecked()){
                        //设置匿名发问
                        questionBean.setAnonymity(true);
                    }else {
                        questionBean.setAnonymity(false);
                    }
                    questionBean.setQuestioner(BmobUser.getCurrentUser(AskQuestionActivity.this, UserBean.class));
                    questionBean.setQuestion_answers(0);
                    questionBean.setQuestion_focus(0);

                    new QuestionManager().askQuestion(AskQuestionActivity.this, questionBean, new QuestionManager.AskQuestionListener() {
                        @Override
                        public void AskQuestionOnSuccess() {
                            Log.i("askQuestion", "success");
                            finish();
                        }

                        @Override
                        public void AskQuestionOnFailure(String failureMSG) {
                            Log.i("askQuestion", "failure");
                        }
                    });
                }
            }
        });

    }
}
